package com.xukang.xkdemospringbootes7;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

public class jsoupUtil {
    public static void main(String[] args) throws IOException {
        String keyword = "java";
        // 要获取的网页
        String url = "https://search.jd.com/Search?keyword="+keyword;
        Document document = Jsoup.parse(new URL(url),30000);
        // doc对象，所有js里能用的都能用
        Element element = document.getElementById("J_goodsList");

        Elements elements= element.getElementsByTag("li");
        for(Element e : elements){
            String imgPath  = e.getElementsByTag("img").eq(0).attr("src");
            String price  = e.getElementsByClass("p-price").eq(0).text();
            String title  = e.getElementsByClass("p-name").eq(0).text();
            System.out.println(imgPath);
            System.out.println(price);
            System.out.println(title);
        }
    }
}
